﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Micrhard.FMCG.Server.Interface;
using Micrhard.FMCG.Server.DataAccess;
using Microhard.Core.DBUtility;
using Micrhard.FMCG.Server.DTO;
using Micrhard.FMCG.Server.Msg;
using Microhard.Core;
using Micrhard.FMCG.Server.Models;

namespace Micrhard.FMCG.Server.Impl
{
    public class PrdMapKAPrdImpl : IPrdMapKAPrd
    {
        public bool IsPrdIsInUsing(Guid prdID, Guid KAID)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var cnt = db.ProductSaleMappings.Count(p => p.ProductID == prdID && p.KAID == KAID);
            return cnt > 0 ? true : false;
        }

        public bool IsCustomeCodeInUsing(string customeCode, Guid KAID)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var cnt = db.ProductSaleMappings.Count(p => p.CustomerCode == customeCode && p.KAID == KAID);
            return cnt > 0 ? true : false;
        }

        public Microhard.Core.DBUtility.ExecutedResult Add(Models.PrductMappingKAPrdModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var op = model.ToEntity();
            op.CreateTime = DateTime.Now;

            try
            {
                db.ProductSaleMappings.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, PrdMappingKAPrdMsg.AddFailed);
                Log.Error("IPrdMapKAPrd.Add", ex);
            }
            return rtn;
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(Models.PrductMappingKAPrdModel model)
        {
            ExecutedResult result = new ExecutedResult();
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var entity = db.ProductSaleMappings.SingleOrDefault(l => l.PrdDepID == model.PrdDepID);
            if (entity != null)
            {
                entity.CustomerCode = model.CustomerCode;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, PrdMappingKAPrdMsg.UpdateFailed);
                Log.Error("IPrdMapKAPrd.Update", ex);
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var entity = db.ProductSaleMappings.Where(l => ids.Contains(l.PrdDepID));
            foreach (var e in entity)
            {
                //是否在使用
                if (e.ProductDisplays.Count == 0)
                {
                    db.ProductSaleMappings.DeleteObject(e);
                }
                else
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, string.Format(PrdMappingKAPrdMsg.IsInUsing, e.CustomerCode));
                }
            }
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, PrdMappingKAPrdMsg.DeleteFailed);
                    Log.Error("IPrdMapKAPrd.Delete", ex);
                }
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(Models.PrductMappingKAPrdModel model)
        {
            throw new NotImplementedException();
        }

        public Models.PrductMappingKAPrdModel GetModelByID(Guid id)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            var entity = db.ProductSaleMappings.SingleOrDefault(m => m.PrdDepID == id);
            if (entity != null)
                return entity.ToModel();
            return null;
        }

        public Models.PrductMappingKAPrdModel GetModelByModel(Models.PrductMappingKAPrdModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.PrductMappingKAPrdModel> Query(Models.PrductMappingKAPrdModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            IEnumerable<ProductSaleMapping> entities = from b in db.ProductSaleMappings
                                                       where b.KAID == Model.KAID
                                                       select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderBy(p => p.CustomerCode).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            if (pagedEntities != null && pagedEntities.Count > 0)
                return pagedEntities.Select(m => m.ToModel()).ToList();
            return new List<PrductMappingKAPrdModel>();
        }

        public List<Models.PrductMappingKAPrdModel> Query(Models.PrductMappingKAPrdModel Model)
        {
            throw new NotImplementedException();
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }


        public List<PrductMappingKAPrdModel> GetSalingPrdsByCompanyIDKAID(Guid companyID, Guid KAID)
        {
            MicrhardFMCGServerEntities db = new MicrhardFMCGServerEntities();
            return (from p in db.ProductSaleMappings
                    where p.Product.CompanyID == companyID && p.KAID == KAID
                    select p.ToModel()).OrderBy(p => p.CustomerCode).ToList();
        }
    }
}
